﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using KlauserswagModel;
using WebShopDAL;

namespace ProjektarbeteKlauserSwag
{
    public partial class ShowProducts : System.Web.UI.Page
    {
        List<OrderRow> cart;
        bool selectedItem;

        protected void Page_Load(object sender, EventArgs e)
        {
            try
            {
                if (!this.IsPostBack)
                {
                    FillArticleList();
                    selectedItem = false;
                    Session["SelectedItem"] = selectedItem;
                }
                if (Session["Order"] != null)
                {
                    cart = Session["Order"] as List<OrderRow>;
                }
                else
                {
                    cart = new List<OrderRow>();
                }
            }
            catch (Exception)
            {
            }
        }

        private void FillArticleList()
        {
            ListBoxArticles.Items.Clear();
            var id = Session["CategoryID"] as Category;
            Session["Store"] = ArticleDB.GetAllArticlesByCategory(id.CategoryID);

            var store = Session["Store"] as List<Article>;
            foreach (var item in store)
            {
                ListBoxArticles.Items.Add(new ListItem("Artikel: " + item.ArticleName + " Pris: " + item.UnitPrice + "Kr", item.ArticleID.ToString()));
            }
        }

        protected void ButtonAddToCart_Click(object sender, EventArgs e)
        {
            try
            {
                if (Session["SelectedItem"] != null)
                {
                    if ((selectedItem = (bool)Session["SelectedItem"]))
                    {
                        var store = Session["Store"] as List<Article>;
                        string item = ListBoxArticles.SelectedValue;
                        bool isInCart = CheckIfInCart(item, int.Parse(TextBoxQuantity.Text));

                        AddArticleToCart(store, item, isInCart);

                        Session["Order"] = cart;

                        ListBoxArticles.Items.Clear();
                        FillArticleList();

                        Response.Redirect("~/ShowProducts.aspx");
                    }
                    else
                    {
                        LabelSelectItems.Visible = true;
                        FillArticleList();
                    }
                }
            }
            catch (Exception)
            {
            }
        }

        private void AddArticleToCart(List<Article> store, string item, bool isInCart)
        {
            if (isInCart == false)
            {
                foreach (Article article in store)
                {
                    if (int.Parse(item) == article.ArticleID)
                    {
                        cart.Add(new OrderRow{
                            Quantity= int.Parse(TextBoxQuantity.Text),
                            ArticleID = article.ArticleID}
                            );
                    }
                }
            }
        }

        protected bool CheckIfInCart(string articleId, int quantity)
        {
            if (cart == null)
            {
                return false;
            }
            else
            {
                foreach (var articleItem in cart)
                {
                    if (int.Parse(articleId) == articleItem.ArticleID)
                    {
                        articleItem.Quantity += quantity;
                        return true;
                    }
                }
                return false;
            }
        }

        protected void ListBoxArticles_SelectedIndexChanged(object sender, EventArgs e)
        {
            try
            {
                selectedItem = true;
                Session["SelectedItem"] = selectedItem;
            }
            catch (Exception)
            {
            }
        }
    }
}